require.config({
    shim:{
        'calendar':{exports:'calendar'}
    },
    paths : {
        //"vue":"lib/vue",
        "jquery":"lib/jquery",
        "cmd":"cmd",
    }
});

require(['jquery','cmd'],function($){

//get top menu
var token = detectToken();
//dtu_id
var dtuID = window.location.search.split("=")[1];

$.ajax({ 
    type:"post", 
    url:urls.moreParams_senior, 
    data: {token:token,dtu_id:dtuID}, 
    dataType: 'json', 
    async : true,
    success:function(result){ 
        if(result.code==1){
            $(".noseeForm_token").val(token);
            $(".noseeForm_dtuid").val(dtuID);
            $(".moreParams_upDatetime").html(result.data.last_update);

            var shoud_forinHtml1 = '';
            if(typeof(result.data.equip[1])!="undefined"){
                $(".factoryCTFbox-each").eq(0).show();
                for(var key in result.data.equip[1]){
                    if(result.data.equip[1][key].is_readonly==2){
                            if(result.data.equip[1][key].param_type==2){
                                if(result.data.equip[1][key].value==0){
                                    shoud_forinHtml1 +='<div class="factoryCTFbox-eachBtm-each clearfix">\n\
                                     <div class="factoryCTF-beeach-l fl boxSize"><input name="" type="text" value="'+result.data.equip[1][key].param_name+'" readonly="true" /></div>\n\
                                     <div class="factoryCTF-beeach-r fl boxSize toggleSwitch" data-flag="0"><input table_param="'+result.data.equip[1][key].table_param+'" value="'+result.data.equip[1][key].value+'" name="" type="text" class="validateInput lockUsefull lock-icon off" /></div>\n\
                                   </div>';
                                }else{
                                  shoud_forinHtml1 +='<div class="factoryCTFbox-eachBtm-each clearfix">\n\
                                     <div class="factoryCTF-beeach-l fl boxSize"><input name="" type="text" value="'+result.data.equip[1][key].param_name+'" readonly="true" /></div>\n\
                                     <div class="factoryCTF-beeach-r fl boxSize toggleSwitch" data-flag="1"><input table_param="'+result.data.equip[1][key].table_param+'" value="'+result.data.equip[1][key].value+'" name="" type="text" class="validateInput lockUsefull lock-icon on" /></div>\n\
                                   </div>';  
                                }

                            }if(result.data.equip[1][key].param_type==0){
                                shoud_forinHtml1 +='<div class="factoryCTFbox-eachBtm-each clearfix">\n\
                                 <div class="factoryCTF-beeach-l fl boxSize"><input name="" type="text" value="'+result.data.equip[1][key].param_name+'" readonly="true" /></div>\n\
                                 <div class="factoryCTF-beeach-r fl boxSize"><input data-min="'+result.data.equip[1][key].data_min+'" data-max="'+result.data.equip[1][key].data_max+'" table_param="'+result.data.equip[1][key].table_param+'" title="'+result.data.equip[1][key].data_min+"到"+result.data.equip[1][key].data_max+'" pattern="'+result.data.equip[1][key].data_format+'" original-val="'+result.data.equip[1][key].value+'" update-val="" class="validateInput" name="" type="text" value="'+result.data.equip[1][key].value+'" /><span>'+result.data.equip[1][key].unit+'</span></div>\n\
                               </div>';
                            }

                    }else{
                        if(result.data.equip[1][key].param_type==2){
                            if(result.data.equip[1][key].value==0){
                                shoud_forinHtml1 +='<div class="factoryCTFbox-eachBtm-each clearfix">\n\
                                     <div class="factoryCTF-beeach-l fl boxSize"><input name="" type="text" value="'+result.data.equip[1][key].param_name+'" readonly="true" /></div>\n\
                                     <div class="factoryCTF-beeach-r fl boxSize toggleSwitch" data-flag="0"><input table_param="'+result.data.equip[1][key].table_param+'" value="'+result.data.equip[1][key].value+'" name="" type="text" class="validateInput lockDisabled lock-icon off" disabled="disabled" /></div>\n\
                                   </div>';
                            }else{
                                shoud_forinHtml1 +='<div class="factoryCTFbox-eachBtm-each clearfix">\n\
                                     <div class="factoryCTF-beeach-l fl boxSize"><input name="" type="text" value="'+result.data.equip[1][key].param_name+'" readonly="true" /></div>\n\
                                     <div class="factoryCTF-beeach-r fl boxSize toggleSwitch" data-flag="1"><input table_param="'+result.data.equip[1][key].table_param+'" value="'+result.data.equip[1][key].value+'" name="" type="text" class="validateInput lockDisabled lock-icon on" disabled="disabled" /></div>\n\
                                   </div>';
                            }

                        }if(result.data.equip[1][key].param_type==0){
                            shoud_forinHtml1 +='<div class="factoryCTFbox-eachBtm-each clearfix">\n\
                                 <div class="factoryCTF-beeach-l fl boxSize"><input name="" type="text" value="'+result.data.equip[1][key].param_name+'" readonly="true" /></div>\n\
                                 <div class="factoryCTF-beeach-r fl boxSize"><input data-min="'+result.data.equip[1][key].data_min+'" data-max="'+result.data.equip[1][key].data_max+'" table_param="'+result.data.equip[1][key].table_param+'" title="'+result.data.equip[1][key].data_min+"到"+result.data.equip[1][key].data_max+'" pattern="'+result.data.equip[1][key].data_format+'" original-val="'+result.data.equip[1][key].value+'" update-val="" class="validateInput" name="" type="text" value="'+result.data.equip[1][key].value+'" disabled="disabled" /><span>'+result.data.equip[1][key].unit+'</span></div>\n\
                               </div>';
                        }
                    }

                }               
            }

            $(".shoud_forinHtml1").html(shoud_forinHtml1);

            $("#moreParamsForm").attr("data-connected",result.data.is_connected);

        }else{
            $.flytip(result.msg);
        }
    } 
});






//============系统管理模块一级二级三级按钮菜单权限控制 START ============
getOnelayerMenu();
//三级菜单控制显示(设备详情页的二级菜单实际上是一个三级菜单，特殊处理)
$.ajax({ 
    type:"post", 
    url:urls.getMachineBtns, 
    data: {token:localStorage.getItem("token"),id:201}, 
    dataType: 'json', 
    async : true, 
    success:function(result){
        var tableTopbarlHtml = '';
        for(var i=0;i<result.data.length;i++){
            var forinVals2 = result.data[i].id;
            switch (forinVals2)
            {
            case 205:
              if(result.data[i].ishide==1){
                tableTopbarlHtml +='<a href="machineDetails.html?dtuid='+dtuID+'" data-id="'+result.data[i].id+'" id="eventsDel" herf="javascript:;">'+result.data[i].name+'</a>';
              }
              break;
            case 206:
              if(result.data[i].ishide==1){
                tableTopbarlHtml +='<a href="'+result.data[i].resKey+'?dtuid='+dtuID+'" data-id="'+result.data[i].id+'" id="eventsDel" herf="javascript:;">'+result.data[i].name+'</a>';
              }
              break;
            case 207:
              if(result.data[i].ishide==1){
                tableTopbarlHtml +='<a href="interactiveLog.html?dtuid='+dtuID+'" data-id="'+result.data[i].id+'" id="eventsDel" herf="javascript:;">'+result.data[i].name+'</a>';
              }
              break;
            case 208:
              if(result.data[i].ishide==1){
                tableTopbarlHtml +='<a href="#" data-id="'+result.data[i].id+'" id="eventsDel" herf="javascript:;">'+result.data[i].name+'</a>';
              }
              break;
            } 

        }
        $(".mianshowbpt-r").html(tableTopbarlHtml);
        $(".mianshowbpt-r a").eq(1).addClass("cur");

    } 
});    
getThreelayerMenu(206,dtuID);
$("[data-id='227']").addClass("cur");
//============系统管理模块一级二级三级按钮菜单权限控制 END ============

var validateInput = function(){
    $("body").on("blur",".validateInput",function(){
        //something modified
        var dataMax = $(this).attr("data-max") == "" ? "" : Number($(this).attr("data-max"));
        var dataMin = $(this).attr("data-min") == "" ? "" : Number($(this).attr("data-min"));
        var itself_pattern = $(this).attr("pattern");
        var itself_value = Number($(this).val());
        var original_val = Number($(this).attr("original-val"));
        var update_val = Number($(this).attr("update-val"));
        var table_param = $(this).attr("table_param");
        var itself_regexString = new RegExp(itself_pattern);
        if(dataMax===""||dataMin===""){
            if(!itself_regexString.test(itself_value)){
                $.flytip("输入格式有误");
                $(this).val(original_val);
                $(this).removeClass("hadChanged").removeAttr("name").css({"color":"#666"});
                return false;
            }
        }else{
            if(!itself_regexString.test(itself_value)||itself_value<dataMin||itself_value>dataMax){
                $.flytip("请输入范围"+dataMin+"到"+dataMax+"之间的值");
                $(this).val(original_val);
                $(this).removeClass("hadChanged").removeAttr("name").css({"color":"#666"});
                return false;
            }
        }
        if(itself_value==original_val||itself_value==update_val){
            $(this).removeClass("hadChanged").removeAttr("name").css({"color":"#666"});
        }else{
            $(this).addClass("hadChanged").attr("name",table_param).css({"color":"#f3af33"});
        }
    })
}

validateInput();
disabledBtnStatus();
$("body").on("click",".lockUsefull",function(){
    if($(this).hasClass("on")){
        $(this).removeClass("on").addClass("off");
    }else{
        $(this).removeClass("off").addClass("on");
    }
})

$("body").on("click",".moreParams_saveBtn",function(){
        var hadChangesParamsNums = $(".hadChanged").length;
        var is_connected = $("#moreParamsForm").attr("data-connected");
        if(hadChangesParamsNums!=0&&is_connected!=0){
            //kissy portol awes
            $.ajax({  
            type: "post",  
            url:urls.saveParams,
            data:$('#moreParamsForm').serialize(),
            async: false,  
            error: function(result) {  
                $.flytip(JSON.parse(result).msg);  
            },  
            success: function(result) { 
                if(JSON.parse(result).code=="1"){
                    // $.flytip(JSON.parse(result).msg);
					saveReload();;
                }else{
                    $.flytip(JSON.parse(result).msg);
                }
            }  
          });
            return false;
        }if(is_connected==0){
            $.flytip("当前设备不在线，无法操作");
            return false;
        }if(hadChangesParamsNums==0){
            $.flytip("当前没有任何修改");
            return false;
        }

})

//moreParams_refreshedReturnVal(dtuID,7);
moreParams_refreshedReturnVal(dtuID);

$.ajax({ 
    type:"post", 
    url:urls.getMachineBtns, 
    data: {token:token,id:215}, 
    dataType: 'json', 
    async : true, 
    success:function(result){
      var moreParamsBtnsHtml1 ='';
      var moreParamsBtnsHtml2 ='';
      for(var i=0;i<result.data.length;i++){
        if(result.data[i].id==230){
            moreParamsBtnsHtml1 += '<span data-id="'+result.data[i].id+'" class="moreParams_refreashedBtn"><i class="xwl i-refresh pdr5"></i>'+result.data[i].btn+'</span>';
        }else{
            moreParamsBtnsHtml2 += '<span data-id="'+result.data[i].id+'" class="moreParams_saveBtn">'+result.data[i].btn+'</span>';
        }
      }
      $(".moreParamsBtnsHtml").html(moreParamsBtnsHtml1+moreParamsBtnsHtml2);

    } 
}); 

$(".seniorBTNs").on("click",function(e){
    var vt = $(this).attr("data-vttype");
    var rorw = $(this).attr("data-rorwtype");
    var key = $(this).parent(".mtcftTdWidth-4").siblings(".mtcftTdWidth-2").find("input").val();
    var val = $(this).parent(".mtcftTdWidth-4").siblings(".mtcftTdWidth-3").find("input").val();
    var eqs = $(this).attr("data-eq");
    $.ajax({ 
        type:"post", 
        url:urls.moreParams_frequencyReadWrite, 
        data: {token:token,dtu_id:dtuID,vt:vt,rorw:rorw,key:key,val:val}, 
        dataType: 'json', 
        async : true, 
        success:function(result){
            $.ajax({ 
                type:"post", 
                url:urls.moreParams_frequencyReadWriteReturnData, 
                //data: {token:token,queryId:result.data.queryId},
                data: {token:token,queryId:11}, 
                dataType: 'json', 
                async : true,
                success:function(result){ 
                    $(".mtcftbtmB").eq(eqs).find(".mtcftTdWidth-2 input").val(result.data[0]);
                    $(".mtcftbtmB").eq(eqs).find(".mtcftTdWidth-3 input").val(result.data[1]);
                } 
            });

        } 
    });    
})


//个人中心下拉
$("#showmyAccountMenu").toggle(
    function(){$("#toggleshowmenu").show();},
    function(){$("#toggleshowmenu").hide();}
);


//$.flytip("zhowdom");$.vloading.show();$.vloading.remove();




//req.js end
})